<?php
/**
 * 管理端项目管理
 * @author qianxun
 *
 */
class AdminPro extends MY_Controller{
    function AdminPro(){
        parent::MY_Controller();
        $this->navbarFocus = 'AdminPro_index';
    }
    /**
     * 管理端项目列表
     */
    function index(){
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>'执行反馈列表');
        //查询条件
        $where = array('isDel'=>0);
        //年份
        $year = (int)$this->input->get('year');
        $year || $year = $this->config->item('nowYear');
        $this->viewData['year'] = $year;
        $year && $where['year'] = $year;
        //时段
        $timeUnit = (int)$this->input->get('timeUnit');
        $this->viewData['timeUnit'] = $timeUnit;
        $timeUnit && $where['timeUnit'] = $timeUnit;
        //项目类型
        $proType = (int)$this->input->get('proType');
        $this->viewData['proType'] = $proType;
        $proType && $where['proTypeId'] = $proType;
        //状态
        if (isset($_GET['proStatus'])){
            if (is_numeric($_GET['proStatus'])){
                $proStatus = (int)$_GET['proStatus'];
                switch ($proStatus){
                    case 0:
                        $where['status'] = 0;
                        break;
                    case 1:
                        $where['status'] = 1;
                        $where['endTime <= '] = time();
                        break;
                    case 2:
                        $where['status'] = 1;
                        $where['endTime > '] = time();
                        break;
                }
            }
        }
        //关键字
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        $keyword && $where['proName'] = array('like', $keyword);
        //载入模型
        $this->load->model('projectModel');
        $count = $this->projectModel->getCount($where);
        //分页
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl($_GET['c'], $_GET['m'], $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        
        $this->viewData['proList'] = $this->projectModel->getNewList($where, $limit, $page, '', 'proId desc');
        $this->load->view("AdminPro/index",$this->viewData);
    }
    /**
     * 新增或编辑执行反馈信息
     */
    function add(){
        $this->load->model('projectModel');
        if ($_POST){
            $proId = (int)$this->input->post('proId');
            $data = array(
                'proName' => $this->input->post('proName'),
                'year' => (int)$this->input->post('year'),
                'timeUnit' => (int)$this->input->post('timeUnit'),
                'proTypeId' => (int)$this->input->post('proTypeId'),
                'status' => 0,
                'storeStatusCount' => serialize(array()),
                'isDel' => 0,
                'updateTime' => time(),
            );
            $data['proName'] || showError('请输入项目名称');
            $data['year'] || showError('请选择所属年份');
            $data['timeUnit'] || showError('请选择所属双月');
            $data['proTypeId'] || showError('请选择项目类型');
            $endTime = $this->input->post('endTime');
            $endTime || showError('请选择项目截至提交时间');
            $data['endTime'] = strtotime($endTime);
            if($proId){//编辑
                if ($this->projectModel->edit(array('proId'=>$proId), $data)){
                    showSuccess('保存执行反馈基础内容成功', printUrl('AdminPro', 'publish',array('proId'=>$proId)));
                }else{
                    showError('保存执行反馈基础内容失败');
                }
            }else{//新增
                $data['startTime'] = time();
                $data['createUserId'] = $this->user['userId'];
                $data['createUserName'] = $this->user['userName'];
                $data['createTime'] = time();
                $proId = $this->projectModel->add($data);
                if ($proId){
                    showSuccess('保存执行反馈基础内容成功', printUrl('AdminPro', 'publish',array('proId'=>$proId)));
                }else{
                    showError('保存执行反馈基础内容失败');
                }
            }
        }else{
            $proId = (int)$this->input->get('proId');
            $this->viewData['year'] = $this->config->item('nowYear');
            $this->viewData['timeUnit'] = $this->timeUnitModel->getNowTimeUnit();
            if ($proId){
                $proData = $this->projectModel->getData(array('proId'=>$proId));
                $proData['status'] == 0 || showError('此执行反馈已发布，禁止编辑执行反馈明细内容');
                $this->viewData['year'] = $proData['year'];
                $this->viewData['timeUnit'] = $proData['timeUnit'];
                $this->viewData['proData'] = $proData;
            }
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>($proId ? '修改':'添加') .'执行反馈项目');
            $this->load->view('AdminPro/add', $this->viewData);
        }
    }
    /**
     * 编辑截至提交时间
     */
    function editEndTime(){
        $this->load->model('projectModel');
        if ($_POST){
            $proId = (int)$this->input->post('proId');
            $proId || showError('请选择项目');
            $data = array(
                //'proName' => $this->input->post('proName'),
                //'year' => (int)$this->input->post('year'),
                //'timeUnit' => (int)$this->input->post('timeUnit'),
                //'proTypeId' => (int)$this->input->post('proTypeId'),
                'updateTime' => time(),
            );
            //$data['proName'] || showError('请输入项目名称');
            //$data['year'] || showError('请选择所属年份');
            //$data['timeUnit'] || showError('请选择所属双月');
            //$data['proTypeId'] || showError('请选择项目类型');
            $endTime = $this->input->post('endTime');
            $endTime || showError('请选择项目截至提交时间');
            $data['endTime'] = strtotime($endTime);
            if ($this->projectModel->edit(array('proId'=>$proId), $data)){
                showSuccess('保存执行反馈基础内容成功');
            }else{
                showError('保存执行反馈基础内容失败');
            }
        }else{
            $proId = (int)$this->input->get('proId');
            if ($proId){
                $proData = $this->projectModel->getData(array('proId'=>$proId));
                $this->viewData['year'] = $proData['year'];
                $this->viewData['timeUnit'] = $proData['timeUnit'];
                $this->viewData['proData'] = $proData;
            }
            $this->load->view('AdminPro/editEndTime', $this->viewData);
        }
    }
    /**
     * 发布执行反馈信息，编辑动作，或删除动作
     */
    function publish(){
        //$this->theme = 'bootstrap';
        $this->load->model('projectModel');
        $this->load->model('proActModel');
        if ($_POST){
            $proId = (int)$this->input->post('proId');
            $proId || showError('请选择要发布的执行反馈项目');
            $actList = $this->proActModel->getActListByProId($proId);
            if (!$actList){
                showError('次反馈无任何动作，禁止发布');
            }
            if ($this->projectModel->edit(array('proId'=>$proId), array('status'=>1))){
                showSuccess('发布执行反馈成功', printUrl('AdminPro', 'index'));
            }else{
                showError('发布执行反馈失败');
            }
        }else{
            $proId = (int)$this->input->get('proId');
            $proId || showError('请选择要编辑反馈明细的执行反馈');
            $proData = $this->projectModel->getNewData(array('proId'=>$proId));
            if (!$this->auth->checkProjectPublish($proData)){
                showError('您无权限发布此执行反馈');
            }
            $this->viewData['proData'] = $proData;
            $this->viewData['proId'] = $proId;
            //动作列表
            $actList = $this->proActModel->getActListByProId($proId);
            $this->viewData['actList'] = $actList;
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>'执行反馈列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'publish',array('proId'=>$proId)),'title'=>'发布执行反馈');
            $this->load->view('AdminPro/publish', $this->viewData);
        }
    }
    /**
     * 查看反馈项目
     */
    function check(){
        $this->load->model('projectModel');
        $this->load->model('proActModel');
        $proId = (int)$this->input->get('proId');
        $proId || showError('请选择要查看的执行反馈');
        $proData = $this->projectModel->getNewData(array('proId'=>$proId));
        $this->viewData['proData'] = $proData;
        $this->viewData['proId'] = $proId;
        //动作列表
        $actList = $this->proActModel->getActListByProId($proId);
        $this->viewData['actList'] = $actList;
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>'执行反馈列表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'publish',array('proId'=>$proId)),'title'=>'发布执行反馈');
        $this->load->view('AdminPro/check', $this->viewData);
    }
    /**
     * 删除反馈项目
     */
    function del() {
        $proId = (int)$this->input->post('proId');
        $proId || showError('请选择要删除的反馈项目');
        $this->load->model('projectModel');
        $findData = $this->projectModel->getData(array('proId'=>$proId));
        $findData || showError('你要删除的反馈项目不存在');
        if (!$this->auth->checkProjectPublish($findData)){
            showError('您无权限删除此执行反馈');
        }
        if ($this->projectModel->edit(array('proId'=>$proId), array('isDel'=>1))){
            showSuccess('删除反馈项目成功！');
        }else{
            showError('删除反馈项目失败！');
        }
    }
    /**
     * 查看专营店反馈
     */
    function storeProList(){
        $proId = (int)$this->input->get('proId');
        $proId || showError('请选择查看专营店反馈的反馈项目');
        $this->load->model('projectModel');
        $proData = $this->projectModel->getNewData(array('proId'=>$proId));
        $proData || showError('反馈项目不存在');
        $this->viewData['proData'] = $proData;
        if (!$this->auth->checkStoreProList($proData)){
            showError('您无权限查看专营店反馈');
        }
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>'执行反馈列表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'storeProList', array('proId'=>$proId)),'title'=>'查看专营店反馈');
        //查询条件
        $where = array('proId'=>$proId);
        //审核状态
        $status = (int)$this->input->get('status');
        if ($status == 0){
            $where['auditRs'] = 0;
        }else{
            $where['auditRs > '] = 0;
        }
        $this->viewData['status'] = $status;
        //审核结果筛选
        $auditRs = (int)$this->input->get('auditRs');
        if ($auditRs){
            $where['auditRs'] = $auditRs;
        }
        $this->viewData['auditRs'] = $auditRs;
        //专营店筛选
        $area = $this->input->get('area');
        $this->viewData['area'] = $area;
        $region = $this->input->get('region');
        $this->viewData['region'] = $region;
        $province = $this->input->get('province');
        $this->viewData['province'] = $province;
        $city = $this->input->get('city');
        $this->viewData['city'] = $city;
        $storeId = $this->input->get('storeId');
        $this->viewData['storeId'] = $storeId;
        $keyword = $this->input->get('keyword');
        $this->viewData['keyword'] = $keyword;
        //查找对应地区列表
        $placeStoreList = $this->sellpointModel->searchStore($area, $region, '', $province, $city, $storeId, $keyword);
        $placeStoreIds = array_keys($placeStoreList);
        $this->viewData['allStoreList'] = $placeStoreList;
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
            $where['storeId'] = array('in', $placeStoreIds);
        }else{
            $where['storeId'] = array('in', array_intersect($viewStoreIds, $placeStoreIds));
        }
        $this->load->model('storeProModel');
        //分页
        $count = $this->storeProModel->getCount($where);
        $this->load->library('pagination');
        $getParam = $_GET;
        unset($getParam['c'], $getParam['m'], $getParam['page']);
        $config['base_url'] = printUrl($_GET['c'], $_GET['m'], $getParam);
        $config['total_rows'] = $count;
        $this->pagination->initialize($config);
        $this->viewData['pager'] = $this->pagination->create_links();
        $page = (int)$this->input->get('page');
        $limit = $this->pagination->per_page;
        
        $this->viewData['storeProList'] = $this->storeProModel->getNewList($where, $limit, $page, '', 'id desc');
        $this->load->view('AdminPro/storeProList', $this->viewData);
    }
    /**
     * 审核专营店答题
     */
    function storeProAudit(){
        $this->load->model('projectModel');
        $this->load->model('storeProModel');
        if ($_POST){
            $id = (int)$this->input->get('id');
            $id || showError('请选择要审核的专营店反馈');
            $findData = $this->storeProModel->getData($id);
            $proData = $this->projectModel->getData(array('proId'=>$findData['proId']));
            $proId = (int)$this->input->post('proId');
            $subtype = (int)$this->input->post('subtype');
            $auditValue = $this->input->post('auditValue');
            $auditRs = $this->storeProModel->reckonAuditRs($auditValue);
            $data = array(
                'auditRole' => $this->user['userRole'],
                'auditUserId' => $this->user['userId'],
                'auditValue' => serialize($auditValue),
                'auditRs' => $auditRs,
                'auditTime' => time(),
            );
            if ($this->storeProModel->edit(array('id'=>$id), $data)){
                //审核结果为不合格时发送邮件
                if ($auditRs == 2 && $findData['storeId']){
                    $storeData = $this->sellpointModel->getStoreIdData($findData['storeId']);
                    if ($storeData['email']){
                        //$emailCon = $this->config->item('emailConfig');
                        //$this->load->library('SMTPMail', $emailCon, 'SMTPMail');
                        $title = '重要！-请查收关于《'.$proData['proName'].'项目》执行反馈不合格的通知';
                        $content = $storeData['storeName'].'专营店，您好！<br><br>您于'.date('Y年m月d日', $findData['subTime']).'上传的关于《'.$proData['proName'].'项目》的执行反馈结果中，存在内容不合格，请尽快提供正确照片，并于'.date('m月d日', $proData['endTime']).'前再次上传，我们将重新审核。<br><br>如对本次检核结果有疑问，请发送申诉邮件至地区支持办公室余聪、市场部崔柯，同时抄送本辖区督导及大区市场经理。<br><br>谢谢！';
                        //$this->SMTPMail->SendMail("dflpvmkt@bluedoorinteractive.cn", $storeData['email'] ,$title, $content, '东风日产营销网');
                        //邮件提醒
                        $this->load->library('email');
                        //发件人
                        $this->email->from('dflpvmkt@bluedoorindex.com', '东风日产营销网');
                        //收件人
                        //$storeData['email'] = 'zhaojianhui129@vip.qq.com';
                        $this->email->to($storeData['email']);
                        //抄送
                        //$this->email->cc(array('ivy.huang@bluedoorindex.com'));
                        //暗送
                        //$this->email->bcc('them@their-example.com');
                        
                        $this->email->subject($title);
                        $this->email->message($content);
                        
                        $this->email->send();
                    }
                }
                //设置报表状态为有更新
                $this->load->model('proReportModel');
                $this->proReportModel->setHavaNew($proId);
                //更新专营店状态数
                $this->load->model('projectModel');
                $this->projectModel->updateStoreStatusCount($proId);
                //更新RMP单店基金标准动作完成率
                //@file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/rmp2016/index.php?c=Crontab&m=updateSingleProcess&storeId='.$findData['storeId'].'&timeUnit='.$findData['timeUnit'].'&year='.$findData['timeUnit']);
                //默认跳转至已审核列表
                $jumpUrl = printUrl('AdminPro', 'storeProList', array('proId'=>$proId,'status'=>1));
                if ($subtype == 1){//如果继续审核下一张
                    $where = array('proId'=>$proId,'auditRs'=>0,'id <'=>$id);
                    //权限内专营店列表
                    $viewStoreIds = $this->userLib->getViewStoreIds();
                    if ($viewStoreIds == '*'){
                        
                    }else{
                        $where['storeId'] = array('in', $viewStoreIds);
                    }
                    $nextStorePro = $this->storeProModel->getList($where, 1, 0, '', 'id desc');
                    $nextStorePro = array_values($nextStorePro);
                    if ($nextStorePro){
                        $jumpUrl = printUrl('AdminPro', 'storeProAudit', array('id'=>$nextStorePro[0]['id']));
                    }
                }
                showSuccess('审核专营店问卷成功', $jumpUrl);
            }else{
                showError('审核专营店问卷失败');
            }
        }else{
            $id = (int)$this->input->get('id');
            $findData = $this->storeProModel->getData(array('id'=>$id));
            $findData || showError('专营店申报数据不存在');
            //$findData['auditRs'] == 0 || showError('此专营店问卷已审核');
            $this->viewData['storeProData'] = $findData;
            //专营店数据
            $storeData = $this->sellpointModel->getStoreIdData($findData['storeId']);
            $this->viewData['storeData'] = $storeData;
            
            $proData = $this->projectModel->getNewData(array('proId'=>$findData['proId'],'isDel'=>0,'status'=>1));
            $proData || showError('项目数据不存在');
            $this->viewData['proData'] = $proData;
            //读取题目数据
            $this->load->model('storeProModel');
            $this->viewData['actList'] = $this->storeProModel->getActList($id);
            //审核选项
            $proActAuditItems = $this->config->item('proActAuditItems');
            $this->viewData['proActAuditItems'] = $proActAuditItems;
            //面包屑
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>'执行反馈列表');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'storeProList', array('proId'=>$findData['proId'],'status'=>0)),'title'=>'查看专营店反馈');
            $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'storeProAudit'),'title'=>'专营店反馈查看');
            $this->load->view('AdminPro/storeProAudit', $this->viewData);
        }
    }
    /**
     * 跳转到下一张问卷
     */
    function storeProAuditNext(){
        $id = (int)$this->input->get('id');
        $id || showError('请选择要审核的专营店反馈');
        $this->load->model('storeProModel');
        $findData = $this->storeProModel->getData($id);
        $proId = (int)$findData['proId'];
        $where = array('proId'=>$proId,'auditRs'=>0,'id <'=>$id);
        //默认跳转至已审核列表
        $jumpUrl = printUrl('AdminPro', 'storeProList', array('proId'=>$proId,'status'=>1));
        //权限内专营店列表
        $viewStoreIds = $this->userLib->getViewStoreIds();
        if ($viewStoreIds == '*'){
        
        }else{
            $where['storeId'] = array('in', $viewStoreIds);
        }
        $nextStorePro = $this->storeProModel->getList($where, 1, 0, '', 'id desc');
        $nextStorePro = array_values($nextStorePro);
        if ($nextStorePro){
            $jumpUrl = printUrl('AdminPro', 'storeProAudit', array('id'=>$nextStorePro[0]['id']));
        }
        showSuccess('正在跳转...', $jumpUrl);
    }
    /**
     * 查看专营店答题
     */
    function storeProCheck(){
        $id = (int)$this->input->get('id');
        $this->load->model('storeProModel');
        $findData = $this->storeProModel->getData(array('id'=>$id));
        $findData || showError('专营店申报数据不存在');
        
        $this->load->model('projectModel');
        $proData = $this->projectModel->getNewData(array('proId'=>$findData['proId'],'isDel'=>0,'status'=>1));
        $proData || showError('项目数据不存在');
        $this->viewData['proData'] = $proData;
        //读取题目数据
        $this->load->model('storeProModel');
        $this->viewData['actList'] = $this->storeProModel->getActList($id);
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>'执行反馈列表');
        //$this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'storeProList', array('proId'=>$findData['proId'],'status'=>1)),'title'=>'查看专营店反馈');
        $this->viewData['breadcrumb'][] = array('url'=>$_SERVER['HTTP_REFERER'],'title'=>'查看专营店反馈');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'storeProCheck', array('id'=>$id)),'title'=>'专营店反馈查看');
        $this->load->view('StorePro/check', $this->viewData);
    }
    /**
     * 查看专营店报告
     */
    function storeProReport(){
        $proId = (int)$this->input->get('proId');
        $proId || showError('请选择查看报告的反馈项目');
        $this->load->model('projectModel');
        $proData = $this->projectModel->getNewData(array('proId'=>$proId));
        $proData || showError('反馈项目不存在');
        $this->viewData['proData'] = $proData;
        if (!$this->auth->checkStoreProReport($proData)){
            showError('您无权限查看专营店报告');
        }
        //读取题目数据
        $this->load->model('proActModel');
        $actList = $this->proActModel->getActListByProId($proId);
        //echo jsonEncode($actList);exit;
        $this->viewData['actList'] = $actList;
        //读取报表数据
        $this->load->model('proReportModel');
        $reportData = $this->proReportModel->getReportData($proId);
        $this->viewData['reportData'] = $reportData;
        //面包屑
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'index'),'title'=>'执行反馈列表');
        $this->viewData['breadcrumb'][] = array('url'=>printUrl('AdminPro', 'storeProReport', array('proId'=>$proId)),'title'=>'查看报告');
        
        $this->load->view('AdminProReport/storeProReport', $this->viewData);
    }
}